package com.ruoyi.api.service.impl;

import com.ruoyi.api.service.ApprovalService;
import com.ruoyi.api.vo.Approval;
import com.ruoyi.vim.mapper.ApprovalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tio.utils.jfinal.P;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    private ApprovalMapper approvalMapper;

    /**
     * 添加用户申请车辆信息,并判断是否成功
     * @param approval
     * @return
     */
    @Override
    public boolean addApproval(Approval approval) {
        boolean a = approvalMapper.adds(approval);
        return a;
    }

    /**
     * 通过用户id进行查询
     * @param userId
     * @return
     */
    @Override
    public List<Approval> selectApproval(Long userId) {
        List<Approval> list = approvalMapper.selectByuserId(userId);
        return list;
    }

    /**
     * 通过对象的参数进行修改
     * @param approval
     */
    @Override
    public void updateApproval(Approval approval) {
        approvalMapper.updateByIds(approval);
    }

    /**
     * 查询所有用户申请信息
     * @return
     */
    @Override
    public List<Approval> selectAll() {
        List<Approval> approvals = approvalMapper.selectAll();
        return approvals;
    }

    /**
     * 通过ids进行多用户删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteById(List<Long> ids) {
        for (Long id : ids) {
            boolean a = approvalMapper.deleteByid(id);
            if (a) {
                return true;
            }
        }
        return false;
    }

    /**
     * 通过id和状态修改用户申请信息
     * @param id
     * @param state
     */
    @Override
    public void updateById(Long id, Integer state) {
        approvalMapper.updates(id,state);
        //判断通过时是否有同一车辆在申请时间段内有交集的申请,有的话做失效处理
        Approval approval = approvalMapper.selectByIds(id);
        List<Approval> approvalList = approvalMapper.selectByCar(approval);
        for (Approval car : approvalList) {
            if((approval.getBeginTime() <= car.getBeginTime() && approval.getEndTime() >= car.getEndTime()) || (approval.getBeginTime() >= car.getBeginTime() && approval.getEndTime() >= car.getEndTime() && approval.getBeginTime() <= car.getEndTime()) || (approval.getBeginTime() <= car.getBeginTime() && approval.getEndTime() <= car.getEndTime() && approval.getEndTime() >= car.getBeginTime())){
                approvalMapper.updateEff(car.getId());
            }
        }
    }

    /**
     * 根据id获取车牌号
     * @param id
     * @return
     */
    @Override
    public String selectById(Long id) {
        String a = approvalMapper.selectId(id);
        return a;
    }

    /**
     * 根据车牌号查询预约此车用户的id
     * @param value
     * @return
     */
    @Override
    public List<Long> selectByValue(String value) {
        List<Long> list = approvalMapper.selectByValue(value);
        return list;
    }

    @Override
    public List<Approval> selectByCarInfo(Approval approval) {
        List<Approval> list = approvalMapper.selectBycarInfo(approval);
        return list;
    }

    @Override
    public void updateByIdEff(Long id) {
        approvalMapper.updateEff(id);
    }

    @Override
    public List<Approval> selectByCar(String carId,Long userId) {
        if(userId == 1){
            List<Approval> list = approvalMapper.selectByCars(carId);
            if(list.isEmpty()){
                return null;
            }
            return list;
        }
        List<Approval> list = approvalMapper.selectByCare(carId,userId);
        if(list.isEmpty()){
            return null;
        }
        return list;
    }

    @Override
    public List<String> selectByUserId(String userId) {
        List<String> list = approvalMapper.selectByUserId(Long.valueOf(userId));
        return list;
    }

    @Override
    public Set<String> selectByUserIds() {
        return approvalMapper.selectByUserIds();
    }

}
